<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>对象过滤示例</title>
    <style>
        body {
            font-family: 'Arial', sans-serif;
            background-color: #f4f4f9;
            color: #333;
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
        }

        .container {
            background-color: white;
            border-radius: 10px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
            padding: 30px;
            width: 500px;
            text-align: center;
        }

        h1 {
            color: #4CAF50;
            font-size: 24px;
            margin-bottom: 20px;
        }

        p {
            font-size: 16px;
            margin-bottom: 20px;
        }

        .output {
            background-color: #f9f9f9;
            border: 1px solid #ddd;
            border-radius: 5px;
            padding: 10px;
            font-size: 16px;
            min-height: 50px;
            display: flex;
            justify-content: center;
            align-items: center;
            color: #4CAF50;
            margin-top: 10px;
            font-weight: bold;
            word-wrap: break-word;
        }

        button {
            background-color: #4CAF50;
            color: white;
            padding: 10px 20px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 16px;
            transition: background-color 0.3s;
        }

        button:hover {
            background-color: #45a049;
        }
    </style>
</head>

<body>

    <div class="container">
        <h1>对象过滤示例</h1>
        <p>点击按钮，过滤对象中的无效值</p>

        <button onclick="filterObject()">过滤对象</button>

        <div class="output" id="output">输出结果将在这里显示</div>

    </div>

    <script>
        // 定义 compactObject 函数，过滤对象中的无效值
        const compactObject = val => {
            const data = Array.isArray(val) ? val.filter(Boolean) : val;
            return Object.keys(data).reduce(
                (acc, key) => {
                    const value = data[key];
                    if (Boolean(value)) {
                        acc[key] = typeof value === 'object' ? compactObject(value) : value;
                    }
                    return acc;
                },
                Array.isArray(val) ? [] : {}
            );
        };

        // 示例对象
        const exampleObject = {
            name: 'Alice',
            age: 0,
            city: '',
            details: {
                country: 'China',
                phone: null,
                address: 'Shanghai',
                emptyField: undefined
            },
            active: false
        };

        // 点击按钮时触发的函数
        function filterObject() {
            const filteredObject = compactObject(exampleObject);
            document.getElementById('output').textContent = `过滤后的对象: ${JSON.stringify(filteredObject, null, 2)}`;
        }
    </script>

</body>

</html>